"use strict";
String.prototype.format = function() {
	let e = Array.prototype.slice.call(arguments),
		t = 0;
	return this.replace(/%s/g, (function(i, r) {
		return e[t++] || ""
	}))
};
class e {
	#e;
	#t;
	#i;
	#r;
	#s = "content-type";
	#a = "application/json";
	#n = "application/x-www-form-urlencoded";
	#o = "text/xml";
	#d;
	#c;
	#l = [];
	#h;
	#u;
	#f;
	#p;
	#m;
	constructor(e, t, i) {
		this.#e = e, this.#t = t, this.#i = i;
		let {
			data: r,
			headers: s = {},
			httpMethod: a,
			queryStringParameters: n = {},
			isBase64Encoded: o,
			body: d
		} = e;
		this.#r = s, this.#h = r, this.#u = a, o && (d = Buffer.from(d, "base64").toString()), this.#p = d, this
			.#m = n, !e.action && e.path && (e.action = "/" == e.path[0] ? e.path.substr(1) : e.path);
		let c = e.action || "";
		c.startsWith("/") && (e.action = c.substr(1)), this.#f = e.action, this.#l = c.split("/")
	}
	request(e = "", t = "") {
		return this.get(e, t)
	}
	get(e = "", t = "") {
		if ("string" != typeof e) throw new Error("name is must be a string");
		return e ? this.#m[e] || t : {
			...this.#m
		}
	}
	post(e = "", t = "") {
		if ("string" != typeof e) throw new Error("name is must be a string");
		let i = {};
		if (this.#p) {
			const e = this.#r[this.#s];
			if (e === this.#n) i = qs.parse(this.#p);
			else {
				if (e == this.#o) return this.#p;
				if (e != this.#a) return this.#p;
				try {
					i = JSON.parse(this.#p)
				} catch (e) {
					return this.#p
				}
			}
		} else i = this.#h;
		return e ? i[e] || t : {
			...i
		}
	}
	pathParam(e = "", t = "") {
		if ("string" != typeof e) throw new Error("name is must be a string");
		let i = [];
		i = this.#i.app_multi_module ? this.#l.slice(3) : this.#l.slice(2);
		const r = {};
		return i.map(((e, t) => {
			t % 2 == 0 && e && (r[e] = i[t + 1])
		})), e ? r[e] || t : r
	}
	param(e = "", t = "") {
		if ("string" != typeof e) throw new Error("name is must be a string");
		let i = this.post();
		i = "[object Object]" === Object.prototype.toString.call(i) ? i : {
			post: i
		};
		let r = Object.assign({}, this.pathParam(), this.get(), i);
		return e ? r[e] || t : r
	}
	get url() {
		return this.#f
	}
	header(e = "") {
		if ("string" != typeof e) throw new Error("name is must be a string");
		return e ? this.#r[e] : {
			...this.#r
		}
	}
	get lang() {
		const e = this.#r.lang || this.#e.lang;
		if (e && this.#i.app_lang_list && !this.#i.app_lang_list.includes(e)) throw new Error("语言参数错误");
		return e || this.#i.app_lang
	}
	get method() {
		return this.#u
	}
	get ip() {
		return this.#t.CLIENTIP || this.#r["x-real-ip"]
	}
	get ua() {
		return this.#t.CLIENTUA || this.#t.ua
	}
	get module() {
		return this.#i.app_multi_module ? this.#l[0] || this.#i.app_module : ""
	}
	get controller() {
		let e = "";
		return e = this.#i.app_multi_module ? this.#l[1] : this.#l[0], e || this.#i.app_controller
	}
	get action() {
		let e = "";
		return e = this.#i.app_multi_module ? this.#l[2] : this.#l[1], e || this.#i.app_action
	}
	get spaceInfo() {
		return this.#t.SPACEINFO
	}
	get appId() {
		return this.#t.APPID || this.#t.appId
	}
	get deviceId() {
		return this.#t.DEVICEID || this.#t.deviceId
	}
	get functionName() {
		return this.#t.FUNCTION_NAME || ""
	}
	get functionType() {
		return this.#t.FUNCTION_TYPE || ""
	}
	get uniIdToken() {
		return this.#e.uniIdToken
	}
	token(e = "") {
		if (!e) return this.#d;
		this.#d = e
	}
	tokenExpired(e = "") {
		if (!e) return this.#c;
		this.#c = e
	}
	get isTriggerTimer() {
		return "Timer" === this.#e.Type
	}
	get event() {
		return this.#e
	}
	get context() {
		return this.#t
	}
}
class t {
	#e;
	#t;
	#g = {
		mpserverlessComposedResponse: !0,
		isBase64Encoded: !1,
		statusCode: 200,
		headers: {
			"content-type": "application/json"
		},
		body: ""
	};
	constructor(e, t) {
		this.#e = e, this.#t = t
	}
	get isHttp() {
		const e = this.#t.env;
		return !(!e || "http" !== e.MP_SOURCE) || !(!this.#e.httpMethod || !this.#e.headers)
	}
	setHeader(e = "", t = "") {
		if ("string" == typeof e && t) this.#g.headers[e] = t;
		else {
			if ("[object Object]" !== Object.prototype.toString.call(e)) throw new Error("设置 header 错误");
			this.#g.headers = e
		}
	}
	setStatus(e) {
		this.#g.statusCode = e
	}
	setBody(e) {
		this.#g.body = e
	}
	get body() {
		return this.isHttp ? ("object" == typeof this.#g.body && (this.#g.body = JSON.stringify(this.#g.body)), this
			.#g) : this.#g.body
	}
}
const i = require("crypto");
var r = {
	generateInviteCode(e) {
		let t = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
			i = [];
		e = e.replace(/-/g, "");
		for (let r = 0; r < 8; r++) {
			let s = e.slice(3 * r, 3 * r + 3),
				a = parseInt(s, 16);
			i.push(t[a % 62])
		}
		return i.join("")
	},
	uuid(e, t) {
		var i, r, s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),
			a = [];
		if (t = t || s.length, e)
			for (i = 0; i < e; i++) a[i] = s[0 | Math.random() * t];
		else
			for (a[8] = a[13] = a[18] = a[23] = "-", a[14] = "4", i = 0; i < 36; i++) a[i] || (r = 0 | 16 * Math
				.random(), a[i] = s[19 == i ? 3 & r | 8 : r]);
		return a.join("")
	},
	random: (e, t) => e >= 0 && t > 0 && t >= e ? Math.floor(Math.random() * (t - e)) + e : 0,
	isUrl: e =>
		/^((https|http|ftp|rtsp|mms):\/\/)(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z].[a-zA-Z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/
		.test(e),
	isEmail: e =>
		/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
		.test(e),
	isChinese: e => /^[\u4e00-\u9fa5]+$/gi.test(e),
	isLetter: e => /^[a-zA-Z]*$/.test(e),
	enOrNum: e => /^[0-9a-zA-Z]*$/g.test(e),
	isMobile: e => /^(?:(?:\+|00)86)?1\d{10}$/.test(e),
	isPhone: e => /^\d{3}-\d{8}$|^\d{4}-\d{7,8}$/.test(e),
	deepClone(e, t) {
		var i, r;
		for (i in r = JSON.stringify(e), t = t || {}, r = JSON.parse(r)) r.hasOwnProperty(i) && (t[i] = r[i]);
		return r = null, t
	},
	caseTransform(e, t = -1) {
		let i = "",
			r = "",
			s = "",
			a = [];
		if (e.includes("-")) {
			s = e.replace(/-/g, "_"), e.split("-").forEach((e => {
				i += e.slice(0, 1).toUpperCase() + e.slice(1)
			})), r = i.slice(0, 1).toLowerCase() + i.slice(1), a = [i, r, s, e]
		} else if (e.includes("_")) {
			s = e.replace(/_/g, "-"), e.split("_").forEach((e => {
				i += e.slice(0, 1).toUpperCase() + e.slice(1)
			})), r = i.slice(0, 1).toLowerCase() + i.slice(1), a = [i, r, e, s]
		} else if (/[A-Z]/.test(e.slice(0, 1))) {
			i = e.slice(0, 1).toLowerCase() + e.slice(1), i.split("").forEach((e => {
				/[A-Z]/.test(e) && (e = e.toLowerCase(), r += "_", s += "-"), r += e, s += e
			})), a = [e, i, r, s]
		} else {
			e.split("").forEach((e => {
				/[A-Z]/.test(e) && (e = e.toLowerCase(), r += "_", s += "-"), r += e, s += e
			})), i = e.slice(0, 1).toUpperCase() + e.slice(1), a = [i, e, r, s]
		}
		return t < 0 || t > 3 ? a : a[t]
	},
	isNumeric: e => /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(e),
	jsonString(e) {
		if ("string" == typeof e) try {
			var t = JSON.parse(e);
			return !("object" != typeof t || !t)
		} catch (e) {
			return !1
		}
		return !1
	},
	isArray: e => "function" == typeof Array.isArray ? Array.isArray(e) : "[object Array]" === Object.prototype
		.toString.call(e),
	isObject: e => "[object Object]" === Object.prototype.toString.call(e),
	isEmpty(e) {
		switch (typeof e) {
			case "undefined":
				return !0;
			case "string":
				if (0 == e.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, "").length) return !0;
				break;
			case "boolean":
				if (!e) return !0;
				break;
			case "number":
				if (0 === e || isNaN(e)) return !0;
				break;
			case "object":
				if (null === e || 0 === e.length) return !0;
				for (var t in e) return !1;
				return !0
		}
		return !1
	},
	getPasswordExtMethod(e, t) {
		switch (e) {
			case "hmac-sha1":
				return {
					verify(e, i, r) {
						if (r !== t.version) return !1;
						const {
							passwordHash: s
						} = this.encrypt(e);
						return s === i
					}, encrypt(e) {
						const {
							type: r,
							version: s
						} = t, a = i.createHmac("sha1", r.toString("ascii"));
						return a.update(e), {
							passwordHash: a.digest("hex"),
							version: s
						}
					}
				};
			case "hmac-sha256":
				return {
					verify(e, r, s) {
						if (s !== t.version) return !1;
						let [, , a, n] = r.split("$");
						if (a = Number(a), !a || !n || isNaN(a)) return !1;
						let o = n.slice(0, a);
						const d = i.createHmac("sha256", o).update(e).digest("hex");
						return `$UNI_ID_HMAC_SHA256$${o.length}$${o}${d}` === r
					}, encrypt(e) {
						const {
							version: r
						} = t, s = i.randomBytes(10).toString("hex"), a = i.createHmac("sha256", s).update(e)
							.digest("hex");
						return {
							passwordHash: `$UNI_ID_HMAC_SHA256$${s.length}$${s}${a}`,
							version: r
						}
					}
				}
		}
	}
};
class s {
	#r = {
		"Content-Type": "application/json;charset=utf-8"
	};
	#y = "json";
	constructor() {}
	setHeader(e = "", t = "") {
		return "[object Object]" === Object.prototype.toString.call(e) ? this.#r = e : e && t && "string" ==
			typeof e && (this.#r[e] = t), this
	}
	setDataType(e) {
		return this.#y = e, this
	}
	get(e, t = {}) {
		return this.request(e, "GET", t)
	}
	post(e, t = {}) {
		return this.request(e, "POST", t)
	}
	async request(e, t = "GET", i = {}) {
		const r = await uniCloud.httpclient.request(e, {
			method: t,
			headers: this.#r,
			dataType: this.#y,
			data: i
		});
		return 200 == r.status ? r.data : r
	}
}
class a {
	constructor(e) {
		this.$dx = e, this.dbCmd = e.db.command
	}
	#b = function(e) {
		let t = {};
		for (let i of e.entries(e)) Object.assign(t, this.#_(i));
		return t
	};
	#w = function(e, t = "=", i) {
		let r = {},
			s = [];
		switch (t) {
			case "=":
			case "eq":
				r[e] = this.dbCmd.eq(i);
				break;
			case "!=":
			case "<>":
			case "neq":
				r[e] = this.dbCmd.neq(i);
				break;
			case ">":
			case "gt":
				r[e] = this.dbCmd.gt(i);
				break;
			case ">=":
			case "gte":
				r[e] = this.dbCmd.gte(i);
				break;
			case "between":
				r[e] = this.dbCmd.gte(i[0]).and(this.dbCmd.lte(i[1]));
				break;
			case "<":
			case "lt":
				r[e] = this.dbCmd.lt(i);
				break;
			case "<=":
			case "lte":
				r[e] = this.dbCmd.lte(i);
				break;
			case "in":
			case "IN":
				s = [], "string" == typeof i && (s = -1 != i.indexOf(",") ? i.split(",") : [i]), Array.isArray(i) &&
					(s = i), r[e] = this.dbCmd.in(s);
				break;
			case "nin":
			case "NIN":
				s = [], "string" == typeof i && (s = -1 != i.indexOf(",") ? i.split(",") : [i]), Array.isArray(i) &&
					(s = i), r[e] = this.dbCmd.nin(s);
				break;
			case "like":
				"string" == typeof i ? r[e] = new RegExp("" + i, "i") : this.$dx.utils.isArray(i) && (r[e] =
					new RegExp(i.join("|"), "i"));
				break;
			case "like%":
				r[e] = new RegExp("^" + i, "i");
				break;
			case "%like":
				r[e] = new RegExp(i + "^", "i");
				break;
			case "contain":
				s = Array.isArray(i)?[...i]:[i];
			    r[e] = this.dbCmd.all(s);
			    break;
			case "contains":
			    s = [], "string" == typeof i && (s = -1 != i.indexOf(",") ? i.split(",") : [i]), Array.isArray(i) &&
				(s = i),r[e] = this.dbCmd.elemMatch(this.dbCmd.in(s));
			    break;
			case "is_empty":
				let t = [];
				Array.isArray(i) || (i = [i]);
				for (let e of i) switch (e) {
					case "null":
					case null:
						t.push(this.dbCmd.eq(null));
						break;
					case "empty":
						t.push(this.dbCmd.eq(""));
						break;
					case "0":
					case 0:
						t.push(this.dbCmd.eq(0));
						break;
					case "exists":
						t.push(this.dbCmd.exists(!1));
						break;
					case "false":
					case !1:
						t.push(this.dbCmd.eq(!1))
				}
				r[e] = this.dbCmd.or(t)
		}
		return r
	};
	#_ = function(e) {
		let t = {};
		for (let [i, r] of Object.entries(e)) this.$dx.utils.isArray(r) && 2 == r.length ? Object.assign(t, this.#w(
			i, r[0], r[1])) : this.$dx.utils.isObject(r) && "string" == typeof r.op && r.op ? Object.assign(t,
			this.#w(i, r.op, r.value)) : Object.assign(t, this.#w(i, "=", r));
		return t
	};
	getWhere(e) {
		if (!e) return {};
		switch (e.length) {
			case 1:
				if (this.$dx.utils.isArray(e[0])) return this.#b(e[0]);
				if (this.$dx.utils.isObject(e[0])) return this.#_(e[0]);
				break;
			case 2:
				return this.$dx.utils.isObject(e[0]) && !0 === e[1] ? e[0] : this.#w(e[0], "=", e[1]);
			case 3:
				return this.#w(...e)
		}
		return {}
	}
	toGetWhere(e) {
		let t = [];
		for (let i of e) t.push(this.getWhere(i));
		return t
	}
}

function n(e, t, i) {
	if (void 0 === e[t] || i.is_array) return e;
	if (Array.isArray(i.bindFields)) {
		for (let r of i.bindFields) "string" == typeof r && (e[r] = e[t][0] && e[t][0][r]);
		Reflect.deleteProperty(e, t)
	} else if (this.isObject(i.bindFields)) {
		for (let [r, s] of Object.entries(i.bindFields)) e[r] = e[t][0] && e[t][0][s];
		Reflect.deleteProperty(e, t)
	} else e[t] = e[t][0] || {};
	return e
}

function o(e, t, i = 1) {
	"string" == typeof t ? t.includes(",") ? t.split(",").forEach((t => {
		e[t] = i
	})) : e[t] = i : Array.isArray(t) && t.forEach((t => {
		t.includes(".") ? t.split(".").forEach((t => {
			e[t] = i
		})) : e[t] = i
	}))
}
const d = require("path"),
	c = require("fs"),
	l = require("koa-compose"),
	h = uniCloud.database();
const u = require("joi"),
	f = u;
var p = {
	Router: class {
		#$ = Symbol((new Date).toDateString());
		constructor(i, c, l = {}) {
			const u = {},
				f = function(e, t = "") {
					return new Proxy((function() {
						this.path = this.path ? d.join(this.path, t) : t
					}), {
						get: function(t, i) {
							let r = new t;
							return r.path ? f(e, d.join(r.path, i)) : t[i]
						},
						apply: function(t, i, r) {
							let s = new t,
								c = s.path.startsWith("service");
							c && !0 === e.$dx.getConfig("app_unify_service") ? s.path = d.join(e
								.app_path, s.path) : s.path = d.join(e.app_module_path, s
								.path);
							let l = d.dirname(s.path) + ".js",
								h = d.basename(s.path);
							if (void 0 === u[l]) {
								let t = e.getStat(l);
								if (!t || !t.isFile()) throw new Error(s.path +
									" is not found");
								u[l] = l
							}
							let f = new(require(l))(e.$dx);
							if (c && (f = function(e) {
									let t = new Proxy(e, {
										get(e, i) {
											const {
												$dx: r
											} = e;
											let s = new a(r),
												d = Symbol.for(
													"dx-model-where"),
												c = Symbol.for(
													"dx-model-where-or"),
												l = Symbol.for(
													"dx-model-field"),
												h = Symbol.for(
													"dx-model-order"),
												u = Symbol.for(
													"dx-model-limit"),
												f = Symbol.for("dx-model-skip"),
												p = Symbol.for("dx-model-with");
											switch (void 0 === e[d] && (e[
													d] = []), void 0 === e[c] &&
												(e[
													c] = []), void 0 === e[l] &&
												(e[l] = {}), void 0 === e[p] &&
												(e[p] = {}), void 0 === e[h] &&
												(e[h] = {}), void 0 === e[u] &&
												(e[u] = 0), void 0 === e[f] && (
													e[f] = 0), i) {
												case "where":
													return function(...i) {
														return e[d].push(i),
															t
													};
												case "whereOr":
													return function(...i) {
														return e[c].push(i),
															t
													};
												case "getQueryWhere":
													return function(...e) {
														return s.getWhere(e)
													};
												case "field":
													return function(i) {
														return o(e[l], i), t
													};
												case "hidden":
													return function(i) {
														return o(e[l], i,
															0), t
													};
												case "order":
													return function(i, r) {
														if (!i || !r || ![
																"asc",
																"desc"
															].includes(r
																.toLowerCase()
															))
															throw new Error(
																"orderby parameter error"
															);
														return e[h][i] = r
															.toLowerCase(),
															t
													};
												case "limit":
													return function(i) {
														if (i = Number(i),
															isNaN(i))
															throw new Error(
																"limit parameter error"
															);
														return e[u] = i, t
													};
												case "skip":
													return function(i) {
														if (i = Number(i),
															isNaN(i))
															throw new Error(
																"skip parameter error"
															);
														return e[f] = i, t
													};
												case "insert":
													return async function(i,
														s = !0) {
														if (!r.utils
															.isObject(i)
														) throw new Error(
															"incorrect data format inserted"
														);
														let a = await e
															.setData(i,
																s),
															n = await t
															.$emit(
																"before_insert",
																a);
														if (!1 === n)
															return n;
														void 0 !== n &&
															(a = n),
															Reflect
															.deleteProperty(
																a, "_id"
															);
														const {
															id: o
														} = await r.db
															.collection(
																e.entity
																.table)
															.add(a);
														return o && (a
																._id =
																o,
																await t
																.$emit(
																	"after_insert",
																	a)),
															o
													};
												case "insertAll":
													return async function(i,
														s = !0) {
														if (!r.utils
															.isArray(i))
															throw new Error(
																"incorrect data format inserted"
															);
														let a = [];
														for (let r of
																i) {
															let i =
																await e
																.setData(
																	r, s
																);
															await t
																.$emit(
																	"before_insert",
																	i),
																Reflect
																.deleteProperty(
																	i,
																	"_id"
																), a
																.push({
																	...
																	i
																})
														}
														let n = await t
															.$emit(
																"before_insert",
																a);
														if (!1 === n)
															return n;
														void 0 !== n &&
															(a = n);
														const {
															ids: o
														} = await r.db
															.collection(
																e.entity
																.table)
															.add(a);
														return o &&
															await t
															.$emit(
																"after_insert",
																a.map(((e,
																		t
																		) =>
																	(e._id =
																		o[
																			t
																			],
																		e
																	)
																))),
															o
													};
												case "delete":
												    
													return async function() {
														let i = s
															.toGetWhere(
																e[d]), a = s
															.toGetWhere(
																e[c]);
														const _ids = i&&i[0]&&i[0]._id&&i[0]._id.operands||[];
														if (!1 ===
															await t
															.$emit(
																"before_delete",_ids
															))
															return;
															let {
																deleted: n
															} = await r
															.db
															.collection(
																e.entity
																.table)
															.where(r.db
																.command
																.or(...
																	a, r
																	.db
																	.command
																	.and(
																		...
																		i
																	)
																))
															.remove();
														return n && t
															.$emit(
																"after_delete",_ids
															), n
													};
												case "deleteAll":
													return async function() {
														return await r
															.db
															.collection(
																e.entity
																.table)
															.where({
																_id: r
																	.db
																	.command
																	.exists(
																		!
																		0
																	)
															}).remove()
													};
												case "update":
													return async function(i,
														a = !1) {
														if (!r.utils
															.isObject(i)
														) throw new Error(
															"incorrect data format updated"
														);
														let n = await e
															.setData(i,
																a),
															o =
															void 0 !== i
															._id && i
															._id;
														o && (n._id = i
															._id);
														let l = await t
															.$emit(
																"before_update",
																n);
														if (!1 === l)
															return l;
														void 0 !== l &&
															(n = l),
															Reflect
															.deleteProperty(
																n, "_id"
															);
														let h = s
															.toGetWhere(
																e[d]),
															u = s
															.toGetWhere(
																e[c]),
															f = !1;
														if ("string" ==
															typeof e
															.update_time &&
															e
															.update_time &&
															!0 === e
															.autoWriteTime &&
															!n[e
																.update_time
																] &&
															e.entity
															.fields
															.some((t =>
																t
																.key ===
																e
																.update_time
															)) && (
																n[e
																	.update_time
																	] =
																(
																	new Date
																	)
																.getTime()
															), o) {
															let {
																updated: t
															} = await r
																.db
																.collection(
																	e
																	.entity
																	.table
																)
																.doc(i
																	._id
																)
																.update(
																	n);
															t && (f = !
																0)
														} else {
															const {
																updated: t
															} = await r
																.db
																.collection(
																	e
																	.entity
																	.table
																)
																.where(r
																	.db
																	.command
																	.or(...
																		u,
																		r
																		.db
																		.command
																		.and(
																			...
																			h
																		)
																	)
																)
																.update(
																	n);
															t && (f = !
																0)
														}
														return e[d]
															.length = 0,
															e[c]
															.length = 0,
															f && await t
															.$emit(
																"after_update", {
																	...
																	n,
																	_id: i
																		._id
																}), f
													};
												case "save":
													return async function(i,
														a = !0) {
														if (!r.utils
															.isObject(i)
														) throw new Error(
															"incorrect data format save"
														);
														i._id && a && (
															a = !1);
														let n = await e
															.setData(i,
																a),
															o = await t
															.$emit(
																"before_write",
																n);
														if (!1 === o)
															return o;
														void 0 !== o &&
															(n = o);
														let l = !1;
														if (i._id) {
															"string" ==
															typeof e
																.update_time &&
																e
																.update_time &&
																!0 === e
																.autoWriteTime &&
																!n[e
																	.update_time
																	] &&
																e.entity
																.fields
																.some((t =>
																	t
																	.key ===
																	e
																	.update_time
																)) &&
																(n[e.update_time] =
																	(
																		new Date
																		)
																	.getTime()
																);
															let {
																updated: t
															} = await r
																.db
																.collection(
																	e
																	.entity
																	.table
																)
																.doc(i
																	._id
																)
																.update(
																	n);
															t && (l = !
																0)
														} else if (e[d]
															.length) {
															"string" ==
															typeof e
																.update_time &&
																e
																.update_time &&
																!0 === e
																.autoWriteTime &&
																!n[e
																	.update_time
																	] &&
																e.entity
																.fields
																.some((t =>
																	t
																	.key ===
																	e
																	.update_time
																)) &&
																(n[e.update_time] =
																	(
																		new Date
																		)
																	.getTime()
																);
															let t = s
																.toGetWhere(
																	e[d]
																),
																i = s
																.toGetWhere(
																	e[c]
																),
																{
																	updated: a
																} =
																await r
																.db
																.collection(
																	e
																	.entity
																	.table
																)
																.where(r
																	.db
																	.command
																	.or(...
																		i,
																		r
																		.db
																		.command
																		.and(
																			...
																			t
																		)
																	)
																)
																.update(
																	n);
															e[d].length =
																0, e[c]
																.length =
																0, a &&
																(l = !0)
														} else {
															let {
																id: t
															} = await r
																.db
																.collection(
																	e
																	.entity
																	.table
																)
																.add(n);
															t && (l = !
																0)
														}
														return l &&
															await t
															.$emit(
																"after_write",
																n), l
													};
												case "select":
													return async function(t = !
														1) {
														let i = s
															.toGetWhere(
																e[d]),
															a = s
															.toGetWhere(
																e[c]);
														e.soft_delete &&
															i.push({
																[e
																	.delete_time
																]: r
																	.db
																	.command
																	.or(r
																		.db
																		.command
																		.eq(
																			null
																			),
																		r
																		.db
																		.command
																		.eq(
																			""
																			),
																		r
																		.db
																		.command
																		.eq(
																			0
																			),
																		r
																		.db
																		.command
																		.eq(!
																			1
																		)
																	)
															});
														let o = r.db
															.collection(
																e.entity
																.table),
															m = Object
															.keys(e[p]),
															g = "get";
														if (m.length) {
															g = "end",
																o = o
																.aggregate(),
																i
																.length &&
																a
																.length ?
																o = o
																.match(r
																	.db
																	.command
																	.or(...
																		i,
																		r
																		.db
																		.command
																		.and(
																			...
																			i
																		)
																	)
																) :
																i
																.length &&
																(o = o
																	.match(
																		r
																		.db
																		.command
																		.and(
																			...
																			i
																		)
																	)
																);
															const t = r
																.db
																.command,
																s = t
																.aggregate;
															for (let i of
																	m) {
																let a,
																	n =
																	e[p]
																	[i],
																	d =
																	s
																	.pipeline();
																a = n
																	.is_array ?
																	t
																	.expr(
																		s
																		.in([`$${n.foreignKey}`,
																			`$$${n.localKeyAlias}`
																		])
																	) :
																	t
																	.expr(
																		s
																		.eq([`$${n.foreignKey}`,
																			`$$${n.localKeyAlias}`
																		])
																	),
																	d =
																	d
																	.match(
																		a
																	),
																	r
																	.utils
																	.isEmpty(
																		n
																		.field
																	) ||
																	(d = d
																		.project(
																			n
																			.field
																		)
																	),
																	"hasMany" ===
																	n
																	.type ?
																	n
																	.limit &&
																	(d = d
																		.limit(
																			n
																			.limit
																		)
																	) :
																	d =
																	d
																	.limit(
																		n
																		.limit
																	),
																	o =
																	o
																	.lookup({
																		from: n
																			.model,
																		let: {
																			[n
																				.localKeyAlias
																			]: `$${n.localKey}`
																		},
																		pipeline: d
																			.done(),
																		as: i
																	})
															}
															if (r.utils
																.isEmpty(
																	e[l]
																) ||
																(o = o
																	.project(
																		e[
																			l
																			]
																	)
																), !
																r.utils
																.isEmpty(
																	e[h]
																)) {
																let
																	t = {};
																for (
																	let [
																		i,
																		r
																	] of Object
																	.entries(
																		e[
																			h
																			]
																	)
																) t[
																		i
																		] =
																	"asc" ===
																	r ?
																	1 :
																	-1;
																o = o
																	.sort(
																		t
																	)
															}
														} else {
															r.utils
																.isEmpty(
																	e[l]
																) ||
																(o = o
																	.field(
																		e[
																			l
																			]
																	)
																), i
																.length &&
																a
																.length ?
																o = o
																.where(r
																	.db
																	.command
																	.or(...
																		i,
																		r
																		.db
																		.command
																		.and(
																			...
																			i
																		)
																	)
																) :
																i
																.length &&
																(o = o
																	.where(
																		r
																		.db
																		.command
																		.and(
																			...
																			i
																		)
																	)
																);
															for (let [t,
																	i
																] of Object
																.entries(
																	e[h]
																))
																o = o
																.orderBy(
																	t, i
																)
														}
														e[f] && (o = o
															.skip(e[
																f
																])
														), e[
															u] && (
															o = o
															.limit(
																e[u]
															));
														let {
															affectedDocs: y,
															data: b
														} = await o[g]
															();
														return e[d]
															.length = 0,
															e[c]
															.length = 0,
															y > 0 ?
															await Promise
															.all(b.map((async i => {
																for (
																	let t of
																		m
																) {
																	let s =
																		e[
																			p
																			]
																		[
																			t
																		];
																	"hasOne" ===
																	s.type &&
																		(i = n
																			.call(
																				r
																				.utils,
																				i,
																				t,
																				s
																			)
																		)
																}
																return t ?
																	i :
																	await e
																	.getData(
																		i
																	)
															}))) : []
													};
												case "count":
													let a = s.toGetWhere(e[d]),
														m = s.toGetWhere(e[c]);
													return async function() {
														let t = r.db
															.collection(
																e.entity
																.table);
														a.length && m
															.length ?
															t = t.where(
																r.db
																.command
																.or(...
																	m, r
																	.db
																	.command
																	.and(
																		...
																		a
																	)
																)) :
															a.length ?
															t = t.where(
																r.db
																.command
																.and(...
																	a)
															) : m
															.length && (
																t = t
																.where(r
																	.db
																	.command
																	.or(...
																		m
																	)
																));
														let {
															total: i
														} = await t
															.count();
														return e[d]
															.length = 0,
															e[c]
															.length = 0,
															i || 0
													};
												case "find":
													return async function(e = !
														1) {
														return (await t
																.select(
																	e))[
																0] ||
															null
													};
												case "setInc":
													return async function(t, i =
														1) {
														let a = s
															.toGetWhere(
																e[d]),
															n = s
															.toGetWhere(
																e[c]),
															o = {};
														o[t] = r.db
															.command
															.inc(i);
														let {
															updated: l
														} = await r.db
															.collection(
																e.entity
																.table)
															.where(r.db
																.command
																.or(...
																	n, r
																	.db
																	.command
																	.and(
																		...
																		a
																	)
																))
															.update(o);
														return l
													};
												case "setDec":
													return function(e, i = 1) {
														return i = i > 0 ? -
															i : Math.abs(i),
															t.setInc(e, i)
													};
												case "with":
													return function(i) {
														if ("string" ==
															typeof i) e[p][
																i
															] = t[i]()
															.getParam();
														else if (Array
															.isArray(i))
															for (let r of i)
																e[p][r] = t[
																	r]()
																.getParam();
														return t
													};
												case "hasOne":
													return function(e, t, i,
														s = !1, a = 1) {
														let n = new Proxy({
															type: "hasOne",
															model: e,
															foreignKey: i,
															localKey: t,
															limit: a,
															is_array: s,
															localKeyAlias: `dx${t}`,
															field: {}
														}, {
															get(e,
																t
																) {
																switch (
																	t
																) {
																	case "bindField":
																		return function(
																			t
																		) {
																			return Array
																				.isArray(
																					t
																				) &&
																				t
																				.length ?
																				e
																				.bindFields =
																				t :
																				"string" ==
																				typeof t ?
																				e
																				.bindFields =
																				t
																				.includes(
																					","
																				) ?
																				t
																				.split(
																					","
																				) :
																				[
																					t
																				] :
																				r
																				.utils
																				.isObject(
																					t
																				) &&
																				(e.bindFields =
																					t
																				),
																				n
																		};
																	case "field":
																		return function(
																			t
																		) {
																			return o(
																					e
																					.field,
																					t
																				),
																				n
																		};
																	case "hidden":
																		return function(
																			t
																		) {
																			return o(
																					e
																					.field,
																					t,
																					0
																				),
																				n
																		};
																	case "getParam":
																		return function() {
																			return e
																		}
																}
															}
														});
														return n
													};
												case "hasMany":
													return function(e, t, i, r =
														0) {
														let s = new Proxy({
															type: "hasMany",
															model: e,
															foreignKey: i,
															localKey: t,
															localKeyAlias: `dx${t}`,
															field: {},
															limit: r
														}, {
															get(e,
																t
																) {
																switch (
																	t
																) {
																	case "field":
																		return function(
																			t
																		) {
																			return o(
																					e
																					.field,
																					t
																				),
																				s
																		};
																	case "hidden":
																		return function(
																			t
																		) {
																			return o(
																					e
																					.field,
																					t,
																					0
																				),
																				s
																		};
																	case "limit":
																		return function(
																			t
																		) {
																			return t =
																				parseInt(
																					t
																				),
																				isNaN(
																					t
																				) &&
																				(t =
																					0
																					),
																				e
																				.limit =
																				t,
																				s
																		};
																	case "getParam":
																		return function() {
																			return e
																		}
																}
															}
														});
														return s
													};
												case "model":
													return r.db.collection(e
														.entity.table);
												default:
													return e[i]
											}
										},
										set() {}
									});
									return t
								}(f)), "function" != typeof f[h]) throw new Error(h +
								" method is not found");
							return f[h](...r)
						}
					})
				};
			this.app_path = "", this.app_module_path = "";
			let p = ["_config", "_lang"],
				m = {},
				g = this,
				y = null;
			"redis" === l.app_cache && (y = uniCloud.redis()), this.$dx = new Proxy({
				_config: l,
				_lang: {
					"successfully added": "添加成功",
					"failed to add": "添加失败",
					"parameter error": "%s参数错误",
					"incorrect data format inserted": "新增的数据,格式错误",
					"record not found": "记录未找到",
					"edit succeeded": "编辑成功",
					"edit failed": "编辑失败",
					"delete failed": "删除失败",
					"deletion succeeded": "删除成功",
					"operation succeeded": "操作成功",
					"operation failed": "操作失败",
					"no permission to operate": "没有权限操作"
				},
				db: h,
				utils: r,
				Curl: s,
				request: new e(i, c, l),
				response: new t(i, c),
				controller: f(g, "controller"),
				service: f(g, "service"),
				redis: y,
				$on(e, t) {
					if ("function" != typeof t) throw new Error("缺少回调函数");
					void 0 === m[e] && (m[e] = []), m[e].push(t)
				},
				$once(e, t) {
					if ("function" != typeof t) throw new Error("缺少回调函数");
					void 0 === m[e] && (m[e] = []);
					let i = this;

					function r(...s) {
						i.$off(e, r), t.apply(g, s)
					}
					r.callback = t, m[e].push(r)
				},
				$emit(e, ...t) {
					if (void 0 !== m[e])
						for (let i of m[e]) i.apply(g, t)
				},
				$off(e, t) {
					if (void 0 !== m[e]) {
						t || (m[e].length = 0);
						for (let i = 0, r = m[e].length; i < r; i++)
							if (m[e][i] === t || m[e][i].callback === t) {
								m[e].splice(i, 1);
								break
							}
					}
				},
				$t: function(e, ...t) {
					let i = this.getLang(e);
					return i ? i.format(...t) : e
				},
				success: (e = "操作成功", t = {}, i = 1e3) => ({
					message: e,
					data: t,
					code: i
				}),
				fail: (e = "操作失败", t = {}, i = 4e3) => ({
					message: e,
					data: t,
					code: i
				}),
				error(e, t = {}, i = 4e3) {
					throw {
						message: e,
						data: t,
						code: i
					}
				},
				async getUserInfo(e) {
					let t = await h.collection("uni-id-users").doc(e).get();
					return t.data.length ? t.data[0] : null
				},
				updateUserInfo: async (e, t) => await h.collection("uni-id-users").doc(e).update(t)
			}, {
				get(e, t) {
					if (p.includes(t)) throw new Error(t + " is a private variable");
					return "getConfig" === t ? function(t) {
						return t ? e._config[t] : {
							...e._config
						}
					} : "getLang" === t ? function(t) {
						return t ? e._lang[t] : ""
					} : "setLang" === t ? function(t, i) {
						if (g.#$ !== t) throw new Error("setLang is a private function");
						e._lang = Object.assign({}, e._lang, i)
					} : "setUtils" === t ? function(t, i) {
						if (g.#$ !== t) throw new Error("setUtils is a private function");
						e.utils = Object.assign({}, e.utils, i)
					} : Reflect.get(e, t)
				},
				set: (e, t, i) => !!["uniID", "auth"].includes(t) && Reflect.set(e, t, i)
			})
		}
		getStat(e) {
			try {
				return c.statSync(e)
			} catch (e) {}
			return !1
		}
		async addMiddleware() {
			let {
				$dx: e
			} = this;
			const t = e.getConfig("app_middleware");
			if (void 0 !== t && e.utils.isArray(t)) {
				let i = [];
				t.map((t => {
					let r = function(t, i, r) {
						const {
							enable: s,
							ignore: a
						} = i;
						if (s) {
							if (e.utils.isEmpty(a)) return t;
							if ("string" == typeof a && !new RegExp("^" + a, "g").test(r))
								return t;
							if (e.utils.isArray(a) && !a.some((e => new RegExp("^" + e, "g")
									.test(r)))) return t
						}
						return !1
					}(t[0], t[1], e.request.url);
					"function" == typeof r && i.push(r)
				})), await l(i)(this)
			}
		}
		async run() {
			try {
				let e, t = this.$dx.getConfig("app_dir"),
					i = this.$dx.getConfig("app_name"),
					r = this.$dx.getConfig("app_helper");
				const s = this.$dx.getConfig("app_multi_module");
				if (!t) throw new Error("未配置 app_dir");
				let a = d.join(t, i);
				if (e = this.getStat(a), !e || !e.isDirectory()) throw new Error(i + " is not found");
				if (this.app_path = a, this.$dx.request.isTriggerTimer) {
					let i = this.$dx.getConfig("app_trigger");
					if (i) switch (typeof i) {
						case "function":
							await i.call(this);
							break;
						case "string":
							const r = d.join(t, i);
							if (e = this.getStat(r), e && e.isFile()) {
								const e = require(r);
								"function" == typeof e && await e.call(this)
							}
					}
					return "end"
				}
				let n = this.$dx.getConfig("app_hooks");
				if (null != n)
					for (let [i, r] of Object.entries(n))
						if ("function" == typeof r) this.$dx.$on(i, r);
						else if ("string" == typeof r) {
					const s = d.join(t, r);
					if (e = this.getStat(s), e && e.isFile()) {
						const e = require(s);
						"function" == typeof e && this.$dx.$on(i, e)
					}
				}
				await this.addMiddleware();
				let o = d.join(t, r + ".js");
				e = this.getStat(o), e && e.isFile() && this.$dx.setUtils(this.#$, require(o)), this.$dx
					.$emit("app_init");
				let c = this.$dx.request.lang,
					l = d.join(d.join(t, "lang"), c + ".js");
				e = this.getStat(l), e && e.isFile() && this.$dx.setLang(this.#$, require(l));
				let h = a;
				if (s) {
					const t = this.$dx.request.module;
					if (console.log("module", t), h = d.join(a, t), e = this.getStat(h), !e || !e
						.isDirectory()) throw new Error("module " + t + " is not found")
				}
				this.app_module_path = h;
				let u = h,
					f = d.join(h, "controller"),
					p = this.$dx.request.controller.replace(/\./g, "/");
				if (e = this.getStat(d.join(f, p + ".js")), !e || !e.isFile()) throw new Error(
					"controller " + p + " is not found");
				const m = require(d.join(f, p + ".js"));
				u = d.join(d.join(d.join(u, "lang"), c), p + ".js"), e = this.getStat(u), e && e.isFile() &&
					this.$dx.setLang(this.#$, require(u));
				const g = new m(this.$dx),
					y = this.$dx.request.action;
				if ("function" != typeof g[y]) throw new Error(`action ${y} is not found`);
				this.$dx.$emit("action_begin");
				let b = await g[y](this.$dx.request.param(), this.$dx.request.header());
				this.$dx.response.setBody(b);
				const _ = this.$dx.response.body;
				return this.$dx.$emit("app_end", _), _
			} catch (e) {
				if (e.code && e.message) return e;
				if (this.$dx.getConfig("app_debug")) throw e
			}
			return "未返回数据"
		}
	},
	Backend: class {
		constructor(e) {
			this.$dx = e, this.abortEarly = !0, this.allowUnknown = !0, this.dto = null, this
				.multiFields = ["switch", "enable"], this.indexWith = [],this.listWith=[], this.fields = []
		}
		get currentService() {
			let e = this.$dx.service,
				t = this.$dx.request.controller;
			t = t.replace(/\./g, "/");
			for (let i of t.split("/")) e = e[i];
			return e
		}
		async add(e) {
			if (null !== this.dto) {
				const t = u.object(this.dto),
					{
						error: i
					} = t.validate(e, {
						abortEarly: this.abortEarly,
						allowUnknown: this.allowUnknown
					});
				if (i) return this.$dx.fail(i.message)
			}
			let t = await this.currentService.insert(e);
			return t ? this.$dx.success(this.$dx.$t("successfully added"), t) : this.$dx.fail(this.$dx.$t(
				"failed to add"))
		}
		async edit(e) {
			if (null !== this.dto) {
				const t = u.object(this.dto),
					{
						error: i
					} = t.validate(e, {
						abortEarly: this.abortEarly,
						allowUnknown: this.allowUnknown
					});
				if (i) return this.$dx.fail(i.message)
			}
			if (!e._id) return this.$dx.fail(this.$dx.$t("parameter error"));
			return await this.currentService.where("_id", e._id).find() ? await this.currentService.update(
				e) ? this.$dx.success(this.$dx.$t("edit succeeded")) : this.$dx.fail(this.$dx.$t(
				"edit failed")) : this.$dx.fail(this.$dx.$t("record not found"))
		}
		async del({
			ids: e,
			id: t
		}) {
			if (!e && !t) return this.$dx.fail(this.$dx.$t("parameter error"));
			let i = e || t;
			return "string" == typeof i && (i = i.includes(",") ? i.split(",") : [i]), await this
				.currentService.where("_id", "in", i).delete() ? this.$dx.success(this.$dx.$t(
					"deletion succeeded")) : this.$dx.fail(this.$dx.$t("delete failed"))
		}
		async index(e) {
			let {
				page: t = 1,
				size: i = 10,
				order: r,
				sort: s,
				selectPage: a
			} = e;
			for (let t of ["page", "size", "order", "sort", "selectPage"]) Reflect.deleteProperty(e, t);
			t -= 1, t = t < 0 ? 0 : t;
			let n = this.currentService.where(e);
			r && "string" == typeof r && (n = n.order(r, ["asc", "ASC"].includes(s) ? "asc" : "desc")), this
				.indexWith.length > 0 && (n = n.with(this.indexWith));
			let o = await n.field(this.fields).limit(i).skip(t * i).select();
			if (a && o.length < 2) {
				const e = await this.currentService.limit(i).field(this.fields).select();
				o = [...o, ...e.filter((e => !o.some((t => t._id === e._id))))]
			}
			let d = await this.currentService.where(e).count();
			return this.$dx.success(this.$dx.$t("operation succeeded"), {
				list: o,
				total: d
			})
		}
		async detail({
			id: e,
			_id: t
		}) {
			if (!e && !t) return this.$dx.fail(this.$dx.$t("parameter error"));
			let i = await this.currentService.where("_id", e || t).find();
			return i ? this.$dx.success(this.$dx.$t("operation succeeded"), i) : this.$dx.fail(this.$dx.$t(
				"record not found"))
		}
		async multi({
			field: e,
			value: t,
			id: i,
			_id: r
		}) {
			if (!e || void 0 === t || !i && !r) return this.$dx.fail(this.$dx.$t("parameter error"));
			if (!this.multiFields.includes(e)) return this.$dx.fail(this.$dx.$t(
				"no permission to operate"));
			let s = i || r;
			if ("string" == typeof s && (s = s.includes(",") ? s.split(",") : [s]), !Array.isArray(s))
				return this.$dx.fail(this.$dx.$t("parameter error"));
			if (s = [...new Set(s)], (await this.currentService.where("_id", "in", s).select()).length !== s
				.length) return this.$dx.fail(this.$dx.$t("record not found"));
			let a = !1;
			return a = 1 == s.length ? await this.currentService.update({
				_id: s[0],
				[e]: t
			}) : await this.currentService.where("_id", "in", s).update({
				[e]: t
			}), a ? this.$dx.success(this.$dx.$t("operation succeeded")) : this.$dx.fail(this.$dx.$t(
				"operation failed"))
		}
		async list(e={}) {
			//return e;
			let t, {
				order: i,
				sort: r,
				limit:limit,
				selectPage: s
			} = e;
			let slist = [],_ids=[];
			if(e._id){
				slist = await this.currentService.where({_id:e._id}).with(this.listWith).select(1000);
				if(typeof e._id=='string'){
					_ids = [e._id]
				}else if(e._id.value&&Array.isArray(e._id.value)){
					_ids = e._id.value;
				}
			}
			for (let t of ["_id","order", "sort", "selectPage","limit"]){
				Reflect.deleteProperty(e, t);
			}
            limit = limit||1e3;
			if(_ids){
				e._id = {op:"nin",value:_ids}
			}
			if (s) {
				if (t = await this.currentService.where(e).with(this.listWith).field(this.fields).order(i || "_id", ["asc",
						"ASC"
					].includes(r) ? "asc" : "desc").select(), s && t.length < 2) {
					const e = await this.currentService.where(e).with(this.listWith).field(this.fields).limit(limit).select();
					t = [...t, ...e.filter((e => !t.some((t => t._id === e._id))))]
				}
				return this.$dx.success(this.$dx.$t("operation succeeded"), [...slist,...t])
			}
			if (t = await this.currentService.with(this.listWith).field(this.fields).where(e).order(i || "_id", ["asc", "ASC"]
					.includes(r) ? "asc" : "desc").limit(limit).select(), limit === t.length&&limit==1e3) {
				let s = await this.currentService.with(this.listWith).field(this.fields).where(e).order(i || "_id", ["asc",
					"ASC"
				].includes(r) ? "asc" : "desc").limit(limit).skip(limit).select();
				t = [...t, ...s]
			}
			return this.$dx.success(this.$dx.$t("operation succeeded"), [...slist,...t])
		}
		
	},
	Controller: class {
		constructor(e) {
			this.$dx = e
		}
		get currentService() {
			let e = this.$dx.service,
				t = this.$dx.request.controller;
			t = t.replace(/\./g, "/");
			for (let i of t.split("/")) e = e[i];
			return e
		}
	},
	Service: class {
		#x = {};
		#v = {};
		__dx__subscribe = {};
		constructor(e) {
			this.$dx = e, this.autoWrite = !0, this.autoWriteTime = !0, this.create_time = "create_date",
				this.update_time = "update_date", this.delete_time = "delete_date", this.soft_delete = !1,
				this.append = [], "function" == typeof this.init && this.init()
		}
		$on(e, t) {
			if ("function" != typeof t) throw new Error("缺少回调函数");
			this.__dx__subscribe[e] = t
		}
		async $emit(e, ...t) {
			if (void 0 !== this.__dx__subscribe[e]) return await this
				.__dx__subscribe[e].apply(this, t)
		}
		#j = function(e) {
			let [t, i] = this.$dx.utils.caseTransform(e), r = "";
			return "function" == typeof this[e] ? r = e : "function" == typeof this[t] ? r = t :
				"function" == typeof this[i] && (r = i), r
		};
		async setAttr(e, t) {
			let i = this.#j("set_" + e + "_attr");
			i && (this.#x[e] = await this[i](t))
		}
		async getAttr(e, t = {}) {
			let i = this.#j("get_" + e + "_attr");
			return "function" == typeof this[i] ? await this[i](t[e], this.$dx.utils.deepClone(t)) : t[e]
		}
		async setData(e, t = !0) {
			if (this.#x = {}, !Array.isArray(this.entity.fields)) throw new Error(
				"entity fields format error");
			let i = !1,
				r = !1;
			return await Promise.all(this.entity.fields.map((async s => {
				let {
					key: a,
					type: n,
					lang: o
				} = s;
				if (void 0 !== e[a]) {
					let t = e[a];
					"number" === n && "string" == typeof e[a] && (t = Number(t), t =
						isNaN(t) ? 0 : t), this.#x[a] = t
				} else if (this.autoWrite && t) switch (n) {
					case "number":
						const e = void 0 === s.default ? 0 : Number(s.default);
						this.#x[a] = isNaN(e) ? 0 : e;
						break;
					case "array":
						this.#x[a] = void 0 === s.default ? [] : s.default;
						break;
					case "object":
						this.#x[a] = void 0 === s.default ? {} : s.default;
						break;
					case "boolean":
						this.#x[a] = void 0 !== s.default && s.default;
						break;
					case "string":
						this.#x[a] = void 0 === s.default ? "" : s.default;
						break;
					case "any":
						this.#x[a] = void 0 === s.default ? null : s.default
				}
				await this.setAttr(a, e[a]), "string" == typeof this.create_time && this
					.create_time && a === this.create_time && (i = !0), "string" ==
					typeof this.update_time && this.update_time && a === this
					.update_time && (r = !0)
			}))), i && (e[this.create_time] && (this.#x[this.create_time] = e[this.create_time]), !this
				.#x[this.create_time] && this.autoWriteTime && t && (this.#x[this.create_time] = Date
					.now())), r && (e[this.update_time] && (this.#x[this.update_time] = e[this
				.update_time]), !this.#x[this.update_time] && this.autoWriteTime && t && (this.#x[
				this.update_time] = Date.now())), {
				...this.#x
			}
		}
		async getData(e) {
			this.#v = {
				...e
			};
			let t = {};
			return await Promise.all(Object.keys(e).map((async i => {
				t[i] = await this.getAttr(i, e)
			}))), this.$dx.utils.isArray(this.append) && await Promise.all(this.append.map((async i => {
				let r = this.#j("get_" + i + "_attr");
				"function" == typeof this[r] && (t[i] = await this[r](null, this.$dx
					.utils.deepClone(e)))
			}))), t
		}
		getOrigin() {
			return {
				...this.#v
			}
		}
	},
	Joi: f
};
module.exports = p;